package com.yanqu.road.dao.impl.union;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UnionInfoDaoImpl extends TempDataObjectDao<UnionInfo> {

	public long getMaxUnionAliasId() {
		String sql = "select ifnull(max(unionAliasId),0) as maxId from t_u_union";
		ResultSet rs = null;
		CallableStatement call = null;
		long maxId = 0;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				maxId = rs.getLong("maxId");
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return maxId;
	}

	public void getUnionInfoMap(Map<String, UnionInfo> unionInfoMap, Map<String, UnionInfo> deleteUnionInfoMap) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_union";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				UnionInfo data = parse(rs);
				if(data.isDelete()) {
					deleteUnionInfoMap.put(data.getUnionUid(), data);
				}else {
					unionInfoMap.put(data.getUnionUid(), data);
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
	}

	public List<UnionInfo> getUnionInfoList(int maxCount) {
		String sql = "select * from t_u_union where isDelete=0 order by totalEarnSpeed+0 desc limit " + maxCount;
		List<UnionInfo> dataList = new ArrayList<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				UnionInfo data = parse(rs);
				dataList.add(data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataList;
	}

	@Override
	protected boolean addData(UnionInfo data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_union(unionUid,unionName,createTime,outNotice,innerNotice,experience,ownerUserId,ownerNickName,ownerJoinTime,wechatCode,"
				+ "qqCode,memberCount,isAllowRandomJoin,totalEarnSpeed,wealth,isDelete,constructUserIdList,modifyTime,unionFightRewardMark,unionFlag,"
				+ "flagModifyTime,constructActive,vipExp,unionAliasId,decoration,rankActivityId,rankAddExp,preUnionFlag,dayJoinNum) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setString(i++, data.getUnionUid());
			call.setString(i++, data.getUnionName());
			call.setLong(i++, data.getCreateTime());
			call.setString(i++, data.getOutNotice());
			call.setString(i++, data.getInnerNotice());
			call.setLong(i++, data.getExperience());
			call.setLong(i++, data.getMasterUserId());
			call.setString(i++, data.getMasterNickName());
			call.setLong(i++, data.getMasterJoinTime());
			call.setString(i++, data.getWechatCode());
			call.setString(i++, data.getQqCode());
			call.setInt(i++, data.getMemberCount());
			call.setBoolean(i++, data.isAllowRandomJoin());
			call.setString(i++, data.getTotalEarnSpeed().toString());
			call.setLong(i++, data.getWealth());
			call.setBoolean(i++, data.isDelete());
			call.setString(i++, data.getConstructUserIdListString());
			call.setLong(i++, data.getModifyTime());
			call.setInt(i++, data.getUnionFightRewardMark());
			call.setInt(i++, data.getUnionFlag());
			call.setLong(i++, data.getFlagModifyTime());
			call.setInt(i++, data.getConstructActive());
			call.setLong(i++, data.getVipExp());
			call.setLong(i++, data.getUnionAliasId());
			call.setString(i++, data.getDecoration());
			call.setInt(i++, data.getRankActivityId());
			call.setLong(i++, data.getRankAddExp());
			call.setInt(i++, data.getPreUnionFlag());
			call.setInt(i++, data.getDayJoinNum());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UnionInfo data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_union SET unionName=?,createTime=?,outNotice=?,innerNotice=?,experience=?,ownerUserId=?,ownerNickName=?,ownerJoinTime=?,"
				+ "wechatCode=?,qqCode=?,memberCount=?,isAllowRandomJoin=?,totalEarnSpeed=?,wealth=?,isDelete=?,constructUserIdList=?,modifyTime=?,"
				+ "unionFightRewardMark=?,unionFlag=?,flagModifyTime=?,constructActive=?,vipExp=?,decoration=?,rankActivityId=?,rankAddExp=?,preUnionFlag = ?,dayJoinNum = ? WHERE unionUid=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setString(i++, data.getUnionName());
			call.setLong(i++, data.getCreateTime());
			call.setString(i++, data.getOutNotice());
			call.setString(i++, data.getInnerNotice());
			call.setLong(i++, data.getExperience());
			call.setLong(i++, data.getMasterUserId());
			call.setString(i++, data.getMasterNickName());
			call.setLong(i++, data.getMasterJoinTime());
			call.setString(i++, data.getWechatCode());
			call.setString(i++, data.getQqCode());
			call.setInt(i++, data.getMemberCount());
			call.setBoolean(i++, data.isAllowRandomJoin());
			call.setString(i++, data.getTotalEarnSpeed().toString());
			call.setLong(i++, data.getWealth());
			call.setBoolean(i++, data.isDelete());
			call.setString(i++, data.getConstructUserIdListString());
			call.setLong(i++, data.getModifyTime());
			call.setInt(i++, data.getUnionFightRewardMark());
			call.setInt(i++, data.getUnionFlag());
			call.setLong(i++, data.getFlagModifyTime());
			call.setInt(i++, data.getConstructActive());
			call.setLong(i++, data.getVipExp());
			call.setString(i++, data.getDecoration());
			call.setInt(i++, data.getRankActivityId());
			call.setLong(i++, data.getRankAddExp());
			call.setInt(i++, data.getPreUnionFlag());
			call.setInt(i++, data.getDayJoinNum());
			call.setString(i++, data.getUnionUid());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UnionInfo parseData(ResultSet rs) throws SQLException {
		UnionInfo data = new UnionInfo();
		data.setUnionUid(rs.getString("unionUid"));
		data.setUnionName(rs.getString("unionName"));
		data.setCreateTime(rs.getLong("createTime"));
		data.setOutNotice(rs.getString("outNotice"));
		data.setInnerNotice(rs.getString("innerNotice"));
		data.setExperience(rs.getLong("experience"));
		data.setMasterUserId(rs.getLong("ownerUserId"));
		data.setMasterNickName(rs.getString("ownerNickName"));
		data.setMasterJoinTime(rs.getLong("ownerJoinTime"));
		data.setWechatCode(rs.getString("wechatCode"));
		data.setQqCode(rs.getString("qqCode"));
		data.setMemberCount(rs.getInt("memberCount"));
		data.setAllowRandomJoin(rs.getBoolean("isAllowRandomJoin"));
		String value = rs.getString("totalEarnSpeed");
		if(!StringUtils.isNullOrEmpty(value)){
			data.setTotalEarnSpeed(new BigInteger(value));
		}
		data.setWealth(rs.getLong("wealth"));
		data.setDelete(rs.getBoolean("isDelete"));
		data.setModifyTime(rs.getLong("modifyTime"));
		data.setUnionFightRewardMark(rs.getInt("unionFightRewardMark"));
		data.setUnionFlag(rs.getInt("unionFlag"));
		data.setFlagModifyTime(rs.getLong("flagModifyTime"));
		data.setConstructActive(rs.getInt("constructActive"));
		data.setVipExp(rs.getLong("vipExp"));
		data.setConstructUserIdList(StringUtils.stringToLongList(rs.getString("constructUserIdList"), ","));
		data.setUnionAliasId(rs.getLong("unionAliasId"));
		value = rs.getString("decoration");
		if(!StringUtils.isNullOrEmpty(value)){
			data.setDecoration(value);
		}
		data.setRankActivityId(rs.getInt("rankActivityId"));
		data.setRankAddExp(rs.getLong("rankAddExp"));
		data.setPreUnionFlag(rs.getInt("preUnionFlag"));
		data.setDayJoinNum(rs.getInt("dayJoinNum"));
		return data;
	}
}
